<template>
  <div class="modal" v-if="dish" @click="handleBackgroundClick">
    <div class="modal-content" @click.stop>
<!--      <span class="close-button" @click="close">&times;</span>-->

      <div class="feedback-container">
        <h3>菜品反馈 - {{ dish.name }}</h3>
        <div class="feedback-list">
          <table class="feedback-table" v-if="feedbacks.length">
            <thead>
            <tr>
              <th>反馈内容</th>
              <th>提交日期</th>
            </tr>
            </thead>
            <tbody>
            <tr v-for="feedback in feedbacks" :key="feedback.feedback_id">
              <td class="feedback-content">{{ feedback.content }}</td>
              <td>{{ feedback.date }}</td>
            </tr>
            </tbody>
          </table>
          <p v-if="!feedbacks.length">暂无反馈。</p>
        </div>
      </div>

    </div>
  </div>
</template>
<script>
import { getFeedbacksByDishId } from '@/api/dishes'; // Import the API function

export default {
  name: 'DishFeedbackCard',
  props: {
    dish: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      feedbacks: [] // Array to store feedback information
    };
  },
  mounted() {
    this.fetchFeedbacks();
    document.addEventListener('keydown', this.handleKeydown);

  },

  beforeDestroy() {
    document.removeEventListener('keydown', this.handleKeydown);
  },

  methods: {
    close() {
      this.$emit('close');
    },

    handleKeydown(e) {
      if (e.key === 'Escape') {
        this.close();
      }
    },
    handleBackgroundClick() {
      this.$emit('close');
    },

    fetchFeedbacks() {
      if (this.dish && this.dish.dish_id) {
        console.log('test'+ this.dish.dish_id)
        getFeedbacksByDishId(this.dish.dish_id)
            .then(response => {
              console.log('API Response:', response);

              if (response && response.data) {
                this.feedbacks = response.data;
              } else {
                console.error('Invalid response structure:', response);
                this.feedbacks = [];
              }
            })
            .catch(error => {
              console.error('Error fetching feedbacks:', error);
              this.feedbacks = [];
            });
      }
    }
  }
};
</script>
<style scoped>

.modal {
  z-index: 1000;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
}

.modal-content {
  background: white;
  padding: 20px;
  border-radius: 10px;
  text-align: center;
  width: 90vw;
  max-width: 900px;
  height: 80vh;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 4px 10px rgba(0,0,0,0.2);
}

.close-button {
  float: right;
  font-size: 1.5em;
  cursor: pointer;
}

.feedback-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.feedback-list {
  width: 100%;
  height: 90%;

}

.feedback-table {
  width: 100%;
  border-collapse: collapse;
}

.feedback-table th,
.feedback-table td {
  border: 1px solid #ddd;
  padding: 8px;
  text-align: left;
}

.feedback-table th {
  background-color: #f4f4f4;
  font-weight: bold;
}

.feedback-table tr:nth-child(even) {
  background-color: #f9f9f9;
}

.feedback-content {
  white-space: pre-wrap; /* 保留换行和空格，并在需要时进行换行 */
}


h3 {
  margin-bottom: 20px;
}
</style>
